import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Function Widget Demo"),
      ),
      body: SimpleDialogWidget(),
    ),
  ));
}

class SimpleDialogWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      child: const Text("对话框"),
      onPressed: () async {
        await showListDialog(context);
      },
    );
  }

  //  弹出对话框
  Future<void> showListDialog(BuildContext context) async {
    int? index = await showDialog<int>(context: context, builder: (BuildContext context){
      var child = Column(
        children: <Widget>[
          const ListTile(title: Text("请选择")),
          Expanded(
            child: ListView.builder(
              itemCount: 30,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  title: Text("$index"),
                  onTap: () => Navigator.of(context).pop(index),
                );
              },
            ),
          )
        ],
      ) ;
      return UnconstrainedBox(
        constrainedAxis: Axis.vertical,
        child: ConstrainedBox(
          constraints: const BoxConstraints(maxWidth: 280),
          child: Material(
            type: MaterialType.card,
            child: child,
          )
        ),
      );
    });
    if (index != null) {
      print("点击了：$index");
    }
  }
}
